﻿
////////////////////////////////////////////////////////////////////////////////////////
var g_oCalendarCn = null; //日历显示窗
var g_oTarget = null; //返回选择日期的目标对象
var g_dtCurSel = null; //当前选择的日期
var g_iTimeType = 1; //1显示日期,2显示时间，3显示日期时间
var g_oOldPart = null; //原选择的Part项
var g_oOldItem = null; //原选择的面板中的项目
var g_oOldButton = null; //原突起的按钮样式的对象
var g_iYearOffset = 0; //点击年后的偏移
//面板显示类型定义
//var PT_CENT="century",
var PT_YEAR = "year", PT_MON = "month", PT_DAY = "day";
var PT_HOUR = "hour", PT_MINU = "minute", PT_SEC = "second";

var g_sCurPanelType = PT_DAY; //当前面板显示的内容

function InitCalendar(oCal) {
    var oBody = oCal.document.body;
    oBody.style.border = "1px solid #D0D0D0";
    oBody.style.backgroundColor = "#52A3E2";
    var dtToday = new Date();
    var sHtml = "<table style='border:0;width:100%;margin-top:4px;'>";
    if ((g_iTimeType & 1) == 1) {
        sHtml += g_dateHtml;
    }
    if ((g_iTimeType & 2) == 2) {
        sHtml += "<tr><td id='PartTime' align='center' style='width:100%;height:20px;'>" +
 	"<a href='#' onclick='parent.Part_Arraw(\"" + PT_HOUR + "\",true);' style='font-family:Marlett;font-size:9pt;color:#ffffff;border:1 solid #52A3E2;text-decoration:none;' onmouseover='parent.MMObj(this,1)' bcr='#52A3E2' onmouseout='parent.MMObj(this,0)'>3</a>" +
 	"<a href='#' id='PartH' onclick='parent.Part_Click(\"" + PT_HOUR + "\")' style='border:1 solid #52A3E2;font-size:10pt;color:#ffffff;font-weight:bold;width:50px;text-align:center;' bcr='#52A3E2' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>00时</a>" +
 	"<a href='#' onclick='parent.Part_Arraw(\"" + PT_HOUR + "\",false);' style='font-family:Marlett;font-size:9pt;color:#ffffff;border:1 solid #52A3E2;text-decoration:none;' onmouseover='parent.MMObj(this,1)' bcr='#52A3E2' onmouseout='parent.MMObj(this,0)'>4</a> " +
 	"<a href='#' onclick='parent.Part_Arraw(\"" + PT_MINU + "\",true);' style='font-family:Marlett;font-size:9pt;color:#ffffff;border:1 solid #52A3E2;text-decoration:none;' onmouseover='parent.MMObj(this,1)' bcr='#52A3E2' onmouseout='parent.MMObj(this,0)'>3</a>" +
 	"<a href='#' id='PartN' onclick='parent.Part_Click(\"" + PT_MINU + "\")' style='border:1 solid #52A3E2;font-size:10pt;color:#ffffff;font-weight:bold;width:auto;text-align:center;' bcr='#52A3E2' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>00分</a>" +
 	"<a href='#' onclick='parent.Part_Arraw(\"" + PT_MINU + "\",false);' style='font-family:Marlett;font-size:9pt;color:#ffffff;border:1 solid #52A3E2;text-decoration:none;' onmouseover='parent.MMObj(this,1)' bcr='#52A3E2' onmouseout='parent.MMObj(this,0)'>4</a> " +
 	"<a href='#' onclick='parent.Part_Arraw(\"" + PT_SEC + "\",true);' style='font-family:Marlett;font-size:9pt;color:#ffffff;border:1 solid #52A3E2;text-decoration:none;' onmouseover='parent.MMObj(this,1)' bcr='#52A3E2' onmouseout='parent.MMObj(this,0)'>3</a>" +
 	"<a href='#' id='PartS' onclick='parent.Part_Click(\"" + PT_SEC + "\")' style='border:1 solid #52A3E2;font-size:10pt;color:#ffffff;font-weight:bold;' bcr='#52A3E2' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>00秒</a>" +
 	"<a href='#' onclick='parent.Part_Arraw(\"" + PT_SEC + "\",false);' style='font-family:Marlett;font-size:9pt;color:#ffffff;border:1 solid #52A3E2;text-decoration:none;' onmouseover='parent.MMObj(this,1)' bcr='#52A3E2' onmouseout='parent.MMObj(this,0)'>4</a>" +
 	"</td></tr>";
    }
    sHtml += "<tr><td id='Panel' style='width:100%;height:206px;margin:0;padding:0;'></td></tr>" +
 	"<tr><td id='Panel2' style='width:100%;height:20px;padding-left:14px;'>" +
 	"<a href='#' onclick='parent.Panel_Today()' style='font-size:10pt;color:#00FF00;font-weight:bold;border:1 solid #52A3E2;' bcr='#52A3E2' title='" + dtToday.getFullYear() + "-" + (dtToday.getMonth() + 1) + "-" + dtToday.getDate() + "' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>[今 天]</a>　" +
 	"<a href='#' onclick='parent.Panel_Ok()' style='font-size:10pt;color:#ffffff;font-weight:bold;border:1 solid #52A3E2;' bcr='#52A3E2' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>[确 定]</a>　" +
 	"<a href='#' onclick='parent.Panel_Clear()' style='font-size:10pt;color:#ffffff;font-weight:bold;border:1 solid #52A3E2;' bcr='#52A3E2' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>[清 空]</a>　" +
 	"<a href='#' onclick='parent.Panel_Close()' style='font-size:10pt;color:#ffffff;font-weight:bold;border:1 solid #52A3E2;' bcr='#52A3E2' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>[取 消]</a>" +
 	"</td></tr></table>";
    oBody.innerHTML = sHtml;
}



//////////////////////////////////////////////////////////
//日历操作函数
//点击项目内容
function Part_Click(sType) {
    g_sCurPanelType = sType;
    g_iYearOffset = 0;
    UpdatePanel();
}
//点击项目左
//bLeft,true向前一个范围，false向后一个范围
function Part_Arraw(sType, bLeft) {
    if (g_dtCurSel == null || isNaN(g_dtCurSel)) g_dtCurSel = new Date();
    var iSel;
    if (sType == PT_YEAR) {
        iSel = g_dtCurSel.getFullYear();
        if (bLeft) iSel--;
        else iSel++;
        g_dtCurSel.setFullYear(iSel);
    } else if (sType == PT_MON) {
        iSel = g_dtCurSel.getMonth();
        if (bLeft) iSel--;
        else iSel++;
        g_dtCurSel.setMonth(iSel);
    } else if (sType == PT_DAY) {
        iSel = g_dtCurSel.getDate();
        if (bLeft) iSel--;
        else iSel++;
        g_dtCurSel.setDate(iSel);
    } else if (sType == PT_HOUR) {
        iSel = g_dtCurSel.getHours();
        if (bLeft) iSel--;
        else iSel++;
        g_dtCurSel.setHours(iSel);
    } else if (sType == PT_MINU) {
        iSel = g_dtCurSel.getMinutes();
        if (bLeft) iSel--;
        else iSel++;
        g_dtCurSel.setMinutes(iSel);
    } else if (sType == PT_SEC) {
        iSel = g_dtCurSel.getSeconds();
        if (bLeft) iSel--;
        else iSel++;
        g_dtCurSel.setSeconds(iSel);
    }
    UpdatePanel();
}

//平移40年
//bUp,true向前移动40年；false向后移动40年
function Panel_Year(bUp) {
    if (bUp) g_iYearOffset -= 5;
    else g_iYearOffset += 5;
    UpdatePanel();
}
//点击面板内容
//iSign==-1表示是早一个范围
//     ==0表示是当前范围
//     ==1表示是晚一个范围
function Panel_Click(sVal, sType, iSign) {
    if (g_dtCurSel == null || isNaN(g_dtCurSel)) g_dtCurSel = new Date();
    var iSel = parseInt(sVal);
    if (iSel < 0) iSel = 0;
    var iTemp;
    if (sType == PT_YEAR) {
        g_dtCurSel.setFullYear(iSel);
        g_sCurPanelType = PT_MON;
    } else if (sType == PT_MON) {
        if (iSign != 0) {
            iTemp = g_dtCurSel.getFullYear();
            if (iSign < 0) iTemp--;
            else if (iSign > 0) iTemp++;
            g_dtCurSel.setFullYear(iTemp);
        }
        g_dtCurSel.setMonth(iSel - 1);
        g_sCurPanelType = PT_DAY;
    } else if (sType == PT_DAY) {
        if (iSign != 0) {
            iTemp = g_dtCurSel.getMonth();
            if (iSign < 0) iTemp--;
            else if (iSign > 0) iTemp++;
            g_dtCurSel.setMonth(iTemp);
        }
        g_dtCurSel.setDate(iSel);
        if (g_iTimeType == 1) {
            Panel_Ok();
            return;
        } else {
            g_sCurPanelType = PT_HOUR;
        }
    } else if (sType == PT_HOUR) {
        if (iSign != 0) {
            iTemp = g_dtCurSel.getDate();
            if (iSign < 0) iTemp--;
            else if (iSign > 0) iTemp++;
            g_dtCurSel.setDate(iTemp);
        }
        g_dtCurSel.setHours(iSel);
        g_sCurPanelType = PT_MINU;
    } else if (sType == PT_MINU) {
        if (iSign != 0) {
            iTemp = g_dtCurSel.getHours();
            if (iSign < 0) iTemp--;
            else if (iSign > 0) iTemp++;
            g_dtCurSel.setHours(iTemp);
        }
        g_dtCurSel.setMinutes(iSel);
        g_sCurPanelType = PT_SEC;
    } else if (sType == PT_SEC) {
        if (iSign != 0) {
            iTemp = g_dtCurSel.getMinutes();
            if (iSign < 0) iTemp--;
            else if (iSign > 0) iTemp++;
            g_dtCurSel.setMinutes(iTemp);
        }
        g_dtCurSel.setSeconds(iSel);

        Panel_Ok();
        return;
    }
    UpdatePanel();
}
//点击清空
function Panel_Clear() {
    g_oCalendarCn.hide();
    if (g_oTarget != null) {
        g_oTarget.value = "";
        g_oTarget.focus();
    }
}
//点击确定
function Panel_Ok() {
    g_oCalendarCn.hide();
    if (g_oTarget != null) {
        var dtToday;
        if (g_dtCurSel == null || isNaN(g_dtCurSel)) dtToday = new Date();
        else dtToday = g_dtCurSel;
        var sDate = "";
        var iVal = "";
        if ((g_iTimeType & 1) == 1) {
            var iYear = dtToday.getFullYear();
            var iMonth = dtToday.getMonth() + 1;
            var iDay = dtToday.getDate();
            sDate += iYear;
            sDate += "-" + (iMonth < 10 ? "0" + iMonth : iMonth);
            sDate += "-" + (iDay < 10 ? "0" + iDay : iDay);
        }
        if ((g_iTimeType & 2) == 2) {
            if (sDate != "") sDate += " ";
            if (g_dtCurSel != null && !isNaN(g_dtCurSel)) {
                iVal = g_dtCurSel.getHours();
                sDate += iVal < 10 ? "0" + iVal : iVal;
                iVal = g_dtCurSel.getMinutes();
                sDate += ":" + (iVal < 10 ? "0" + iVal : iVal);
                iVal = g_dtCurSel.getSeconds();
                sDate += ":" + (iVal < 10 ? "0" + iVal : iVal);
            }
            else {
                sDate += "00:00:00";
            }
        }
        g_oTarget.value = sDate;
        g_oTarget.focus();
        if (g_oTarget.onchange)
            g_oTarget.onchange();
    }
}
//点击今天
function Panel_Today() {
    g_oCalendarCn.hide();
    if (g_oTarget != null) {
        var dtToday = new Date();
        var sDate = "";
        if ((g_iTimeType & 1) == 1) {
            sDate += dtToday.getFullYear() + "-" + (dtToday.getMonth() + 1) + "-" + dtToday.getDate();
        }
        if ((g_iTimeType & 2) == 2) {
            if (sDate != "") sDate += " ";
            var iVal;
            iVal = dtToday.getHours();
            sDate += iVal < 10 ? "0" + iVal : iVal;
            iVal = dtToday.getMinutes();
            sDate += ":" + (iVal < 10 ? "0" + iVal : iVal);
            iVal = dtToday.getSeconds();
            sDate += ":" + (iVal < 10 ? "0" + iVal : iVal);
        }
        g_oTarget.value = sDate;
        g_oTarget.focus();
    }
}
//点击“取消”
function Panel_Close() {
    g_oCalendarCn.hide();
}

function MMObj(oButton, bIn) {
    if (g_oOldButton != null && g_oOldButton != oButton) {
        if (g_oOldButton.bcr) {
            g_oOldButton.style.borderWidth = "1";
            g_oOldButton.style.borderStyle = "solid";
            g_oOldButton.style.borderColor = g_oOldButton.bcr;
        } else {
            g_oOldButton.style.borderWidth = "0";
            g_oOldButton.style.borderStyle = "";
            g_oOldButton.style.borderColor = "";
        }
    }
    if (bIn) {
        g_oOldButton = oButton;
        if (g_oOldButton != null) {
            g_oOldButton.style.borderWidth = "1";
            g_oOldButton.style.borderStyle = "solid";
            g_oOldButton.style.borderColor = "#FFFFFF #808080 #808080 #FFFFFF";
        }
    } else {
        if (oButton != null) {
            if (g_oOldButton.bcr) { //保留边框
                g_oOldButton.style.borderWidth = "1";
                g_oOldButton.style.borderStyle = "solid";
                g_oOldButton.style.borderColor = g_oOldButton.bcr;
            } else {
                g_oOldButton.style.borderWidth = "0";
                g_oOldButton.style.borderStyle = "";
                g_oOldButton.style.borderColor = "";
            }
        }
        g_oOldButton = null;
    }
}